package com.example.blogsystem.service;

import com.example.blogsystem.constant.Constant;
import com.example.blogsystem.mapper.BlogMapper;
import com.example.blogsystem.mapper.UserMapper;
import com.example.blogsystem.model.Blog;
import com.example.blogsystem.model.Result;
import com.example.blogsystem.model.User;
import com.example.blogsystem.utils.AddSecretUtil;
import com.example.blogsystem.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private AddSecretUtil addSecretUtil;

    //注册
    public Result<Object> registerLogic(String userName, String password) {
        Result<Object> result = new Result<>();
        log.info("userName:{}", userName);
        log.info("password:{}", password);
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            log.error("signIn接口:用户或密码为空");
            return result.fail("用户或密码为空");
        }
        //检查用户名是否符合要求
        if(userName.length() < 6 || userName.length() > 15) {
            log.error("用户名不符合要求");
            return result.unStandard(-4);
        }
        //检查密码是否符合要求
        if(password.length() < 6 || password.length() > 15) {
            log.error("密码不符合要求");
            return result.unStandard(-3);
        }

        //进行加密
        String finalPassword = addSecretUtil.addSecret(password);

        //保存到数据库中
        Integer ret = userMapper.insertUser(userName, finalPassword);
        log.info("ret:{}", ret);
        if(ret < 1) {
            log.error("账号和密码数据库保存失败");
            result.fail("数据库保存失败");
        }
        log.info("数据库保存成功");
        return result.success();
    }

    //查询用户信息
    public Result<String> checkUser(String userName, String password) {
        Result<String> result = new Result<>();
        log.info("参数userName:{}", userName);
        log.info("参数password:{}", password);

        //判断用户名和密码是否为空
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            log.error("login接口:用户名或密码错误");
            return result.fail("用户名或密码为空");
        }

        //查询用户是否存在
        User user = userMapper.checkUser(userName);
        if(user == null) {
            log.error("login接口:用户不存在");
            return result.fail("用户不存在");
        }
        String sqlPassword = user.getPassword();
        //校验数据库中的密码
        boolean isTrue = addSecretUtil.verifyPassword(password, sqlPassword);

        if(!isTrue) {
            log.error("login接口:密码错误");
            return result.fail("密码错误");
        }

        //构造token
        Map<String, Object> map = new HashMap<>();
        map.put(Constant.USER_ID, user.getId());
        map.put(Constant.USER_USERNAME, user.getUserName());
        String token = JwtUtil.getToken(map);
        log.info("构造的token:{}", token);
        return result.success(token);
    }

    // 查询当前登录用户信息
    public Result<User> getUserInfoLogic(HttpServletRequest request) {
        Result<User> result = new Result<>();
        //1. 获取token
        //2. 获取token中的userId
        String userToken = request.getHeader(Constant.USER_TOKEN);
        Integer userId = JwtUtil.getUserIdFromToken(userToken);
        if(userId == null) {
            return result.fail("token中的id为空");
        }
        //3.根据id获取用户信息
        User user = userMapper.userById(userId);
        return result.success(user);
    }


    public User queryAuthorByBlogId(Integer blogId) {
        //1. 根据博客id获取作者id
        //2. 根据作者id获取作者信息
        Blog blog = blogMapper.selBlogById(blogId);
        if(blog == null) return null;
        Integer userId = blog.getUserId();
        if(userId == null) return null;
        User user = userMapper.userById(userId);
        if(user == null) return null;
        return user;
    }
}
